package controllers

import (
	"encoding/json"
	"strconv"
	"supershare/backend/models"

	"github.com/beego/beego/v2/server/web"
)

// CleaningRecordController 保洁记录控制器
type CleaningRecordController struct {
	web.Controller
}

// GetOne 获取单个保洁记录
// @Title GetOne
// @Description get cleaning record by id
// @Param	id	path	string	true	"The key for staticblock"
// @Success 200 {object} models.CleaningRecord
// @Failure 403 :id is empty
// @router /:id [get]
func (c *CleaningRecordController) GetOne() {
	id, _ := strconv.ParseInt(c.Ctx.Input.Param(":id"), 10, 64)
	record, err := models.GetCleaningRecordById(id)
	if err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
	} else {
		c.Data["json"] = SuccessResponse(record)
	}
	c.ServeJSON()
}

// List 获取保洁记录列表
// @Title List
// @Description get cleaning record list
// @Param	page	query	int	false	"Page number"
// @Param	pageSize	query	int	false	"Page size"
// @Param	store_id	query	int64	false	"Store ID"
// @Param	venue_id	query	int64	false	"Venue ID"
// @Param	cleaner_id	query	int64	false	"Cleaner ID"
// @Param	status	query	int	false	"Status"
// @Success 200 {object} map[string]interface{}
// @router / [get]
func (c *CleaningRecordController) List() {
	page, _ := c.GetInt("page", 1)
	pageSize, _ := c.GetInt("pageSize", 10)
	filters := make(map[string]interface{})

	if storeId, err := c.GetInt64("store_id"); err == nil {
		filters["store_id"] = storeId
	}
	if venueId, err := c.GetInt64("venue_id"); err == nil {
		filters["venue_id"] = venueId
	}
	if cleanerId, err := c.GetInt64("cleaner_id"); err == nil {
		filters["cleaner_id"] = cleanerId
	}
	if status, err := c.GetInt8("status"); err == nil {
		filters["status"] = status
	}

	records, total, err := models.GetCleaningRecordList(page, pageSize, filters)
	if err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
	} else {
		c.Data["json"] = SuccessResponse(map[string]interface{}{
			"list":  records,
			"total": total,
		})
	}
	c.ServeJSON()
}

// Create 创建保洁记录
// @Title Create
// @Description create cleaning record
// @Param	body	body	models.CleaningRecord	true	"body for cleaning record content"
// @Success 201 {int} models.CleaningRecord.Id
// @Failure 403 body is empty
// @router / [post]
func (c *CleaningRecordController) Create() {
	var record models.CleaningRecord
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &record); err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
		c.ServeJSON()
		return
	}
	id, err := models.AddCleaningRecord(&record)
	if err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
	} else {
		c.Data["json"] = SuccessResponse(id)
	}
	c.ServeJSON()
}

// Update 更新保洁记录
// @Title Update
// @Description update the cleaning record
// @Param	id	path	string	true	"The id you want to update"
// @Param	body	body	models.CleaningRecord	true	"body for cleaning record content"
// @Success 200 {object} models.CleaningRecord
// @Failure 403 :id is not int
// @router /:id [put]
func (c *CleaningRecordController) Update() {
	id, _ := strconv.ParseInt(c.Ctx.Input.Param(":id"), 10, 64)
	var record models.CleaningRecord
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &record); err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
		c.ServeJSON()
		return
	}
	record.Id = id
	if err := models.UpdateCleaningRecord(&record); err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
	} else {
		c.Data["json"] = SuccessResponse("Updated successfully")
	}
	c.ServeJSON()
}
